package net.mingsoft.mdiy.aop;

import net.mingsoft.basic.aop.*;
import org.springframework.stereotype.*;
import org.aspectj.lang.*;
import net.mingsoft.basic.util.*;
import net.mingsoft.mdiy.constant.e.*;
import net.mingsoft.mdiy.bean.*;
import org.aspectj.lang.annotation.*;

@Component
@Aspect
public class ModelAop extends BaseAop
{
    @Before("execution(* net.mingsoft.mdiy.biz.impl.ModelBizImpl.importConfig(..)) || execution(* net.mingsoft.mdiy.biz.impl.ModelBizImpl.importModel(..)) || execution(* net.mingsoft.mdiy.biz.impl.ModelBizImpl.updateConfig(..))")
    public void updateModelName(final JoinPoint jp) {
        if (BasicUtil.getWebsiteApp() != null) {
            final Object[] args = jp.getArgs();
            if (!ModelCustomTypeEnum.CONFIG.getLabel().equals(args[0])) {
                final String appId = BasicUtil.getWebsiteApp().getAppId();
                final ModelJsonBean modelJsonBean = (ModelJsonBean)args[1];
                final String tableName = modelJsonBean.getTableName();
                modelJsonBean.setTableName(tableName + "_" + appId);
                String sql = modelJsonBean.getSql();
                sql = sql.replaceAll("\\{model}" + tableName, "{model}" + modelJsonBean.getTableName());
                modelJsonBean.setSql(sql);
            }
        }
    }
}
